<style>
	.button-box{
		cursor: pointer;
	}
	.button-box .panel-body{
		text-align: center;
		font-size: 20px;
		font-weight: bolder;
	}
</style>
<script src="https://res.wx.qq.com/wwopen/js/jsapi/jweixin-1.0.0.js"></script>
<script src="https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js"></script>
<script src="/assets/libs/jquery/dist/jquery.min.js"></script>
<div class="panel panel-default panel-intro">
    <div id="waiting" class="panel-heading">
        <div class="alert alert-danger-light">
            <p id="alert_msg">正在获取客户信息中...请稍等</p>
        </div>
    </div>
	<div id="choose" style="display: none;">
		<div class="panel bg-blue-gradient no-border button-box" onclick="linkCustomer()"><div class="panel-body">关联客户</div></div>
		<div class="panel bg-red-gradient no-border button-box" onclick="createCustomer()"><div class="panel-body">新建客户</div></div>
	</div>
</div>



<script>
	var userId = 0;
    wx.config({$wx_config});
    wx.ready(function(){
        var ag_config={$ag_config};
        ag_config.success=function(res) {
            // 回调
            wx.invoke('getCurExternalContact', {
            }, function(res){
                if(res.err_msg == "getCurExternalContact:ok"){
                    userId = res.userId ; //返回当前外部联系人userId
                    $(function(){
						$.ajax({
							//请求方式
							type : "GET",
							//请求的媒体类型
							contentType: "application/json;charset=UTF-8",
							//请求地址
							url : "{:url('facrm/workweixin.index/hasCustomerId')}?userid="+userId,
							success:function(result){
								if (result.code==1){
									if(result.data){
										window.location.href="{:url('facrm/customer.index/edit')}/ids/"+result.data+"?ref=addtabs";
										return;
									}else{
										$('#waiting').hide();
										$('#choose').show();
									}
								}else{
    								$('#alert_msg').text(result.msg);
								}
							},
							error:function(result){
								$('#alert_msg').text(result.msg);
							}
						})
                    });
                }else {
                    //错误处理
                    $('#alert_msg').text('没有getCurExternalContact权限');
                }
            });
        };
        ag_config.fail=function(res) {
            if(res.errMsg.indexOf('function not exist') > -1){
                $('#alert_msg').text('版本过低请升级')
            }
        };
        wx.agentConfig(ag_config);
    });

	function createCustomer(){
		$.ajax({
			//请求方式
			type: "POST",
			//请求的媒体类型
			contentType: "application/json;charset=UTF-8",
			//请求地址
			url: "{:url('facrm/workweixin.index/getCustomerId')}?userid=" + userId,
			//数据，json字符串
			//请求成功
			success: function(result){
				if(result.code == 1 && result.data){
					window.location.href = "{:url('facrm/customer.index/edit')}/ids/" + result.data + "?ref=addtabs";
					return;
				}
			},
			//请求失败，包含具体的错误信息
			error: function(e){
				$('#alert_msg').text('获取用户信息失败');
			}
		});
	}

	function linkCustomer(){
		$.ajax({
			//请求方式
			type: "POST",
			//请求的媒体类型
			contentType: "application/json;charset=UTF-8",
			//请求地址
			url: "{:url('facrm/workweixin.index/createContact')}?userid=" + userId,
			//请求成功
			success: function(result){
				if(result.code == 1 && result.data){
					window.location.href = "{:url('facrm/qywx.contacts/edit')}/action/link_customer/ids/" + result.data + "?ref=addtabs";
					return;
				}
			},
			//请求失败，包含具体的错误信息
			error: function(e){
				$('#alert_msg').text('获取用户信息失败');
			}
		})
	}
</script>


